/*****************************************************
Copyright (C) 2020. All rights reserved.
File name     ：TestMain.c
Version       ：v1.0
Author        ：zhengqijun
Date          ：2020-05-09
Function List ：
Description   ：main function
******************************************************/

#include <stdio.h>  // for printf
#include "PairingHeap.h"

#define MaxSize (50)

// PrintPairHeap for testing
static void PrintDepth(ElementType X, int depth);
static void PrintPairHeap(PriorityQueue H, int depth);

void PrintDepth(ElementType X, int depth) {
    while (depth > 0) {
        printf("    ");
        --depth;
    }
    printf("%02d\n", X);
}

void PrintPairHeap(PriorityQueue H, int depth) {
    if (IsEmpty(H)) {
        printf("PriorityQueue is empty!\n");
        return;
    }

    if (!IsEmpty(LeftChild(H))) {
        PrintPairHeap(LeftChild(H), depth + 1);
    }

    PrintDepth(Retrieve(H), depth);

    if (!IsEmpty(NextSibling(H))) {
        PrintPairHeap(NextSibling(H), depth);
    }
}

// main function
int main(void) {
    PriorityQueue H;
    Position P[MaxSize];
    int i;
    int j;
    int Min;

    H = Initialize();

    if (IsEmpty(H)) {
        printf("PriorityQueue is empty!\n");
    } else {
        printf("PriorityQueue is not empty!\n");
    }

    printf("Insert pairing heap: [ ");
    for (i = 1, j = MaxSize / 2; i < MaxSize; i += 3) {
        printf("%02d ", j);
        H = Insert(j, H, &P[j]);
        j = (j + 21) % MaxSize;
    }
    printf("]\n");

    printf("Print pairing heap:\n");
    PrintPairHeap(H, 0);
    printf("\n");

    printf("Find min element is %d in pairing heap!\n", FindMin(H));

    H = DeleteMin(&Min, H);
    printf("Delete min element is %d in pairing heap!\n", Min);

    printf("Print pairing heap:\n");
    PrintPairHeap(H, 0);
    printf("\n");

    printf("Decrease key 22 by 20 in pairing heap!\n");
    H = DecreaseKey(P[22], 20, H);

    printf("Print pairing heap:\n");
    PrintPairHeap(H, 0);
    printf("\n");

    H = Destroy(H);

    return 0;
}

// EOF

